<?php
namespace Admin\Models\res;
use Admin\Models\BaseModel;
use Helpers\User;


class XhresArticle extends BaseModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(type="integer", length=11, nullable=false)
     */
    public $iArtId;


    /**
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iUserId;

    /**
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bMassage;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iArtType;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iSchoolId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tStartTime;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=true)
     */
    public $sTitle;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mText;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     * @var string
     * @Column(type="integer", nullable=true)
     * create by xjq 20180504
     */
    public $sTag;


    /**
     * @var integer
     * create by xjq 20180509
     */
    public $bExcellent;

    public $sExMonth;

    public $sTargetIds;

    /**
     * 状态
     * @var integer
     * create by xjq 20180607
     */
    public $iState;

    /**
     * @var string
     * create by xjq 20180607
     */
    public $mDesc;

    /**
     * 等级
     *
     * @var integer
     * create by xjq 20180615
     */
    public $iLevel;
    /**
     * 是否退回
     * @var integer
     * create by xjq 20180615
     */
    public $bLock;


    public $sSubject;



    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSchema("xh_webmanage");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHRes_Article';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhresArticle[]|XhresArticle
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhresArticle
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public function beforeCreate()
    {
        $userId  = User::getId();
        $time    = date('Y-m-d H:i:s');
        $this->tCreateDate = $this->tCreateDate ?: $time;
        $this->iCreateUserId = $this->iCreateUserId ?: $userId;
        $this->tLastModifyDate = $this->tLastModifyDate ?: $time;
        $this->iLastModifyUserId = $this->iLastModifyUserId ?: $userId;
    }

    public function save($data = null, $whiteList = null, $recordUpdateInfo = true)
    {
        if ($recordUpdateInfo) {
            parent::beforeUpdate();
        } else {
            $this->beforeUpdate();
        }
        return parent::save($data, $whiteList);
    }

    public function beforeUpdate()
    {
        return true;
    }

    /**
     * 获取统计数据
     *
     * @return array
     *
     * create by sqb 20190110
     */
    public static function getData($startTime1 = null, $startTime2 = null, $endTime1 = null, $endTime2 = null)
    {
        $search = [
            '语文' => ['语文', '文学', '古诗', '诗词', '诗', '文言文', '金秋诗', '词王', '字词'],
            '数学' => ['数学', '速算', '口算'],
            '英语' => ['英语', '英语单词', '口语'],
            '科学' => ['科学', '化学']
        ];

        $conditions = 'bDelete = 0 and iArtType = 9';
        if ($startTime1) {
            $conditions .= " and tStartTime >= '$startTime1'";
        }
        if ($startTime2) {
            $conditions .= " and tStartTime <= '$startTime2'";
        }
        if ($endTime1) {
            $conditions .= " and tEndTime >= '$endTime1'";
        }
        if ($endTime2) {
            $conditions .= " and tEndTime <= '$endTime2'";
        }

        $article = self::find([
            'conditions' => $conditions,
            'columns' => 'iArtId, sTitle'
        ])->toArray();

        $stat = ['语文' => 0, '数学' => 0, '英语' => 0, '科学' => 0, '其它' => 0];
        foreach ($article as $item) {
            $i = 0;
            foreach ($search as $subject => $keys) {
                foreach ($keys as $key) {
                    $match = preg_match('/'.$key.'/', $item['sTitle']);
                    if ($match > 0) {
                        $i++;
                        $stat[$subject]++;
                        break;
                    }
                }
            }
            if ($i == 0) {
                $stat['其它']++;
            }
        }

        $data = [];
        foreach ($stat as $subject => $number) {
            $data[] = [
                'subject' => $subject,
                'number' => $number
            ];
        }

        return ['xList' => array_keys($stat), 'yList' => array_values($stat), 'data' => $data, 'total' => array_sum(array_values($stat))];
    }
}
